package com.neulogistics.contrib.procuredcenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateVendorCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateVendorCommand;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.Vendor;
import com.neulogistics.contrib.procuredcenter.domain.repository.IVendorRepository;
import com.neulogistics.contrib.procuredcenter.domain.repository.IVendorRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class UpdateVendorCommandHandler implements IRequestHandler<UpdateVendorCommand, BaseResponse> {
    @Resource
    IVendorRepository vendorRepository;

    @Override
    public BaseResponse handle(UpdateVendorCommand command) throws Exception {
        
        Vendor vendor = vendorRepository.findById(command.vendorId);

        if(vendor == null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }

        Vendor vendorToUpDate = new Vendor();
        BeanUtils.copyProperties(command, vendorToUpDate);

        vendorRepository.save(vendorToUpDate);

        return ResultUtils.success(null);
    }
}
